<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd">添加信息</el-button>
    </div>

    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">信息标题：</span>
        <el-input placeholder="请输入信息标题" v-model="searchParams.message_title" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">信息内容：</span>
        <el-input placeholder="请输入信息内容" v-model="searchParams.message_content" @keyup.enter.native="handleSearch"
                  clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">是否已发送：</span>
        <el-select v-model="searchParams.is_send" placeholder="请选择是否显示" clearable>
          <el-option label="是" :value="1"/>
          <el-option label="否" :value="2"/>
        </el-select>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">创建时间：</span>
        <el-date-picker
            v-model="searchParamsTime"
            type="datetimerange"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            date-format="YYYY/MM/DD ddd"
            time-format="A hh:mm:ss"
        />
      </div>
      <div class="task-one__btn clearfix">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="message_title" label="信息标题"></el-table-column>
      <el-table-column prop="message_content" label="信息内容"></el-table-column>
      <el-table-column prop="created_at" label="创建开始时间"></el-table-column>
      <el-table-column prop="is_send" label="是否已发送">
        <template #default="{ row }">
          <el-text v-if="row.is_send==1" type="success">已发送</el-text>
          <el-text v-if="row.is_send==2" type="warning">未发送</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="user_type_label" label="发送用户"></el-table-column>
      <el-table-column prop="send_time_label" label="发送时间"></el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <div style="display: flex;justify-content: space-between;">
            <el-button link type="primary" @click="handleEdit(row,{message_id:row.message_id})" v-if="row.is_send==2">编辑</el-button>
            <el-dropdown @command="handleSend" trigger="click" v-if="row.is_send==2">
              <el-button link type="primary">发送给</el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item :command="item.id+'|'+row.message_id+'|'+row.message_title" v-for="(item, index) in user_type_list">{{item.title}}</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                           @confirm="handleDelete(row)">
              <template #reference>
                <el-button link type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </div>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px"
               :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="信息标题" prop="message_title" required>
          <el-input v-model.trim="dialogForm.message_title" clearable placeholder="请输入信息标题"/>
        </el-form-item>

        <el-form-item label="信息内容" prop="message_content" required>
          <el-input v-model.trim="dialogForm.message_content" clearable placeholder="请输入信息内容" type="textarea"
                    :rows="4"/>
        </el-form-item>

        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
import {EluiChinaAreaDht} from "elui-china-area-dht";
import chinaAreaData from "china-area-data";

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

dialogForm.message_title = '';
dialogForm.message_content = '';

apiPath.value = 'admin/message/index?';
dialogParams.addApiPath = 'admin/message/create';
dialogParams.editApiPath = 'admin/message/update';

const user_type_list = [
  {
    id:1,
    title:'所有用户'
  },
  {
    id:2,
    title:'用户'
  },
  {
    id:3,
    title:'管家'
  },
];
const searchParamsTime = ref('');
watch(searchParamsTime,(newV, oldV)=>{
  if(!newV){
    searchParams.s_time = ''
    searchParams.e_time = ''
  }else{
    searchParams.s_time = newV[0]
    searchParams.e_time = newV[1]
  }
})
onMounted(() => {
  getData()
})

const dialogFormRules = reactive({
  message_title: [{
    required: true,
    message: "请输入信息标题",
    trigger: "blur"
  }],
  message_content: [{
    required: true,
    message: "请输入信息内容",
    trigger: "blur"
  }]
})
const handleDelete = (row) => {
  handleAction('admin/message/delete', {
    message_id: row.message_id,
  })
}

const handleSend = (d) => {
  let [user_type,message_id,message_title] = d.split('|');
  let user_type_title = user_type_list.find(v=>v.id == user_type).title;
  proxy.$confirm(`确定将信息【${message_title}】发送给【${user_type_title}】？`, "温馨提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
      .then(() => {
        handleAction('admin/message/send', {
          message_id:Number(message_id),
          user_type:Number(user_type)
        })
      })
      .catch(() => {})
}
</script>

<style lang="less">

</style>

